package com.syq.controller;

import com.syq.service.SysUserService;
import com.syq.utils.R;
import enums.HospitalExceptionEnums;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * @ClassName: LoginController
 * @Description: TODO
 * @Create by: syq
 * @Date: 2022/7/20 20:54
 */

@RestController
public class LoginController {

    private Logger log= LoggerFactory.getLogger(LoginController.class);


    //登录
    @PostMapping("/sel/doLogin")
    public R doLogin(String loginname, String pwd){
        //1.校验请求参数
        if (StringUtils.isEmpty(loginname)||StringUtils.isEmpty(pwd)){
            log.info("【登录功能那个】 请求参数异常，用户名和密码都必须为必填项");
            return R.error(HospitalExceptionEnums.REQUEST_PARAMETER_ERROR,"用户名和密码都必须为必填项");
        }

        //获取subject主题
        Subject subject = SecurityUtils.getSubject();

        //执行认证
        subject.login(new UsernamePasswordToken(loginname,pwd));

        //登陆成功
        return R.ok("登陆成功！");
    }

    @GetMapping("/sel/logout")
    public R logout( ){
        Subject subject = SecurityUtils.getSubject();
        subject.logout();

        return R.ok("退出登陆成功");
    }
}
